The Update Operator ( | =) Of Python Dictionary Class

Operator Name:

The update operator ( |= )

Overview:

The update operator ( |= ) updates the contents of the dictionary instance(known as the current object, self, this and in other names) with the contents from an another dictionary instance d2 or an iterable contaning itreables of name-value pairs.

Operands:

d1 - The current dictionary
d2 - The dictionary from which d1 needs to be updated

Return Value:

A dict instance containing the updated name-value pairs.

Example:

# Example Python program that updates a dictionary
# from another mapping
original = {"S1":62.5,
            "S2":110.5,
            "S3":125.1,
            "S4":220.8,
            "S5":37.5,
            };

reconciled = {"S1":62.5,
              "S2":110.5,
              "S3":125.1,
              "S4":220.8,
              "S5":37.5,
              "S6":293.1,
            };

original.update(reconciled);
print(reconciled);

Output:

{'S1': 62.5, 'S2': 110.5, 'S3': 125.1, 'S4': 220.8, 'S5': 37.5, 'S6': 293.1}

 


Copyright 2023 © pythontic.com